package jast;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.apifan.common.random.source.AreaSource;
import com.apifan.common.random.source.InternetSource;
import com.apifan.common.random.source.PersonInfoSource;
import jast.util.CompanySocialUtil;
import jast.util.Config;
import jast.util.es.ESDataInfo;
import jast.util.es.ElasticSearchDataConvert;
import jast.util.sync.ElasticSearchClientServiceImpl;

import java.io.File;
import java.time.LocalDate;
import java.util.List;

public class Application {

    private static boolean website = false;
    public static void main(String[] args) throws Exception {
        for (String index : Config.INDEX_LIST.split(",")) {
            ElasticSearchClientServiceImpl elasticSearchClientService = new ElasticSearchClientServiceImpl();
            for (int i=1;; i++) {
                String operator_cdn_index = elasticSearchClientService.query(index, 100, i);
                ESDataInfo esDataInfo = ElasticSearchDataConvert.toESDataInfo(operator_cdn_index);
                List<?> rows = esDataInfo.getRows();
                if(rows.size()==0){
                    break;
                }

                for (Object row : rows) {
                    JSONObject jsonObject = JSONUtil.parseObj(row);
                    if(website){
                        //网站假数据修改
                        dataUpdate(jsonObject,index);
                    }
                    String id = jsonObject.getStr("es_id!@#$");
                    jsonObject.remove("es_id!@#$");
                    String purRequest = ElasticSearchDataConvert.getPurRequest(Config.ES_REST_IP, JSONUtil.toJsonStr(jsonObject), index, id);
                    System.out.println("同步索引:"+index+"同步数据id:"+id);
                    System.out.println(purRequest);
                    FileUtil.writeString(purRequest,Config.DATA_SAVE_DIR+File.separator+index+ File.separator+"_"+index+"_"+ UUID.fastUUID()+".sh","UTF-8");
                }
            }
        }

    }


    public static void dataUpdate(JSONObject jsonObject,String index){
        switch (index){
            case "website_info_index":

                break;
            case "website_subject_index":
                jsonObject.put("user_name", PersonInfoSource.getInstance().randomChineseName());
                jsonObject.put("phone", PersonInfoSource.getInstance().randomChineseMobile());
                jsonObject.put("legal_user_name", PersonInfoSource.getInstance().randomChineseName());
                jsonObject.put("email_address", InternetSource.getInstance().randomEmail(15));
                jsonObject.put("office_phone", AreaSource.getInstance().randomPhoneNumber("北京", "-"));
                LocalDate beginDate = LocalDate.of(1970,11,11);
                LocalDate endDate = LocalDate.of(1988,12,12);
                String id1 = PersonInfoSource.getInstance().randomMaleIdCard("北京", beginDate, endDate);
                jsonObject.put("certificate_code", id1);
                jsonObject.put("comp_certificate_code", jsonObject.getStr("comp_certificate_code").replaceFirst("5","6").replaceFirst("3","8"));
                break;
            case "website_contacts_index":
                beginDate = LocalDate.of(1970,11,11);
                endDate = LocalDate.of(1988,12,12);
                id1 = PersonInfoSource.getInstance().randomMaleIdCard("北京", beginDate, endDate);
                jsonObject.put("user_name", PersonInfoSource.getInstance().randomChineseName());
                jsonObject.put("email_address", InternetSource.getInstance().randomEmail(15));
                jsonObject.put("phone", PersonInfoSource.getInstance().randomChineseMobile());
                jsonObject.put("certificate_code", id1);
                break;
        }
    }


}
